package com.example.bzh.gamecat.ui.message.innerfriend;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.bzh.gamecat.MyApp;
import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.ui.message.innerfriend.friendgroup.GroupActivity;
import com.example.bzh.gamecat.ui.message.innerfriend.friendvertification.VertificationActivity;
import com.example.bzh.gamecat.ui.message.innerfriend.service.ServiceActivity;
import com.example.bzh.gamecat.ui.message.innerfriend.subscription.SubscriptionActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageFriendFragment extends Fragment {


    @BindView(R.id.ll_friend_new)
    LinearLayout llFriendNew;
    @BindView(R.id.ll_friend_group)
    LinearLayout llFriendGroup;
    @BindView(R.id.ll_friend_subscription)
    LinearLayout llFriendSubscription;
    @BindView(R.id.ll_friend_service)
    LinearLayout llFriendService;
    @BindView(R.id.lv_friend_friend)
    ListView lvFriendFriend;
    @BindView(R.id.iv_tips_newfriend)
    ImageView ivTipsNewfriend;
    private MessageReceiver receiver;

    public MessageFriendFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message_friend, container, false);
        ButterKnife.bind(this, view);
        receiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("message");
        getActivity().registerReceiver(receiver, filter);
        return view;
    }

    @OnClick({R.id.ll_friend_new, R.id.ll_friend_group, R.id.ll_friend_subscription, R.id.ll_friend_service})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_friend_new:
                startActivity(new Intent(getActivity(), VertificationActivity.class));
                ivTipsNewfriend.setVisibility(View.INVISIBLE);
                MyApp.setFlag(false);
                break;
            case R.id.ll_friend_group:
                startActivity(new Intent(getActivity(), GroupActivity.class));
                break;
            case R.id.ll_friend_subscription:
                startActivity(new Intent(getActivity(), SubscriptionActivity.class));
                break;
            case R.id.ll_friend_service:
                startActivity(new Intent(getActivity(), ServiceActivity.class));
                break;
        }
    }

    class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("message".equals(action)) {
                ivTipsNewfriend.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(receiver);
    }
}
